﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using SetSailDal;
using SetSailModel;
using System.IO;
using System.Diagnostics;

namespace SetSailReadFileTools
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(((System.ComponentModel.Component)(this)));
            this.skinEngine1.SkinFile = Application.StartupPath + "//RealOne.ssk";
        }
        private void Login_Load(object sender, EventArgs e)
        {
            label10.ForeColor = Color.Blue;
            label11.ForeColor = Color.FromArgb(192, 0, 0);

            //记住密码
            bool is_save = Convert.ToBoolean(SetSailReadFileTools.Properties.Settings.Default.IsSavePwd);
            string UserName = Convert.ToString(SetSailReadFileTools.Properties.Settings.Default.UserName);
            string Password = Convert.ToString(SetSailReadFileTools.Properties.Settings.Default.Password);
            //赋值默认值
            IsSavePwd.Checked = is_save;
            Txt_i_Login.Text = UserName.Trim();
            Txt_i_Pwd.Text = Password.Trim();
            string a = "程序集版本：" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            string b = Application.ProductVersion.ToString();
            label10.Text = "版本号：【" + b + "】";
        }

        /// <summary>
        /// 登录——登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_i_login_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Txt_i_Login.Text.Trim()))
            {
                MessageBox.Show("请输入账号");
                return;
            }

            if (string.IsNullOrWhiteSpace(Txt_i_Pwd.Text.Trim()))
            {
                MessageBox.Show("请输入密码");
                return;
            }

            LH_YHXX ly = new LH_YHXX();
            ly.DLZH = Txt_i_Login.Text.Trim();
            ly.DLMM = Common.MD5Encrypt(Txt_i_Pwd.Text).Trim();
            var result = LoginDal.Login(ly);
            if (result.Code == ResultCode.Success)
            {
                SavePwd();
                CookieData.getInstance().YHXX = result.Data;
                this.Hide();
                Main m = new Main();
                m.Show();
            }
            else
            {
                MessageBox.Show(result.Message);
            }
        }

        /// <summary>
        /// 登录——退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_i_Exit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        /// <summary>
        /// 注册——登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Txt_r_Login.Text.Trim()))
            {
                MessageBox.Show("请输入账号");
                return;
            }

            if (Txt_r_Login.Text.Trim().Length < 2)
            {
                MessageBox.Show("账号过短，不能低于两位");
                return;
            }

            if (string.IsNullOrWhiteSpace(Txt_r_Pwd.Text.Trim()))
            {
                MessageBox.Show("请输入密码");
                return;
            }

            if (Txt_r_Pwd.Text.Trim().Length < 6)
            {
                MessageBox.Show("密码不能低于6位");
                return;
            }

            if (string.IsNullOrWhiteSpace(Txt_r_Pwd2.Text.Trim()))
            {
                MessageBox.Show("请输入确认密码");
                return;
            }
            if (Txt_r_Pwd.Text.Trim() != Txt_r_Pwd2.Text.Trim())
            {
                MessageBox.Show("两次密码输入不一致");
                return;
            }
            LH_ZCSH lz = new LH_ZCSH();
            lz.ZCZH = Txt_r_Login.Text.Trim();
            lz.ZCMM = Common.MD5Encrypt(Txt_r_Pwd.Text).Trim();
            lz.QQ = TxtQQ.Text.Trim();
            lz.YY = TxtYY.Text.Trim();
            var result = LoginDal.Register(lz);
            if (result.Code == ResultCode.Success)
            {
                MessageBox.Show("注册申请已提交，请联系管理员审核");
                Txt_r_Login.Text = "";
                Txt_r_Pwd.Text = "";
                Txt_r_Pwd2.Text = "";
                TxtQQ.Text = "";
                TxtYY.Text = "";
            }
            else
            {
                MessageBox.Show(result.Message);
            }
        }

        /// <summary>
        /// 注册——退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton3_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        /// <summary>
        /// 默认焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_Activated(object sender, EventArgs e)
        {
            Txt_i_Login.Focus();
        }

        private void SavePwd()
        {
            if (IsSavePwd.Checked)
            {
                Properties.Settings.Default.IsSavePwd = true;
                Properties.Settings.Default.UserName = Txt_i_Login.Text.Trim();
                Properties.Settings.Default.Password = Txt_i_Pwd.Text.Trim();
            }
            else
            {
                Properties.Settings.Default.IsSavePwd = false;
                Properties.Settings.Default.UserName = Txt_i_Login.Text.Trim();
                Properties.Settings.Default.Password = string.Empty;
            }
            Properties.Settings.Default.Save();
        }
    }
}
